import * as THREE from 'three';
import { BaseCharacter } from './BaseCharacter';
import { AnimationManager } from '../core/animation/AnimationManager';

class TestCharacter extends BaseCharacter {
    constructor(options = {}) {
        super(options);
        
        // 移动相关
        this.moveSpeed = 0.1;
        this.isMoving = false;
        this.moveDirection = new THREE.Vector3();
        
        // 设置测试角色的基础属性
        this.stats = {
            ...this.stats,
            health: 150,
            attack: 15,
            defense: 8,
            speed: 8
        };
        
        // 动画相关
        this.animationTime = 0;
        this.attackDuration = 0.2;
        this.isAttacking = false;
        this.animations = new AnimationManager(this);
    }

    // 重写loadModel方法，使用简单几何体
    async loadModel() {
        // 创建根节点
        this.mesh = new THREE.Group();
        this.mesh.name = 'characterMesh';  // 添加名称
        
        // 创建角色主体
        const geometry = new THREE.CapsuleGeometry(0.5, 1, 4, 8);
        const material = new THREE.MeshStandardMaterial({
            color: 0x00ff00,
            roughness: 0.7,
            metalness: 0.3
        });
        
        const body = new THREE.Mesh(geometry, material);
        body.name = 'body';  // 添加名称
        
        // 添加武器（简单立方体）
        const weaponGeometry = new THREE.BoxGeometry(0.1, 2, 0.1);
        const weaponMaterial = new THREE.MeshStandardMaterial({
            color: 0x808080,
            roughness: 0.5,
            metalness: 0.8
        });
        const weapon = new THREE.Mesh(weaponGeometry, weaponMaterial);
        weapon.name = 'weapon';  // 添加名称
        weapon.position.set(0.7, 0.5, 0);
        weapon.rotation.z = Math.PI / 4;
        
        // 组装角色
        this.mesh.add(body);
        this.mesh.add(weapon);
        
        // 设置阴影
        this.mesh.traverse((node) => {
            if (node.isMesh) {
                node.castShadow = true;
                node.receiveShadow = true;
            }
        });

        // 创建测试动画
        this.createTestAnimations();
        
        return this.animationRoot;
    }

    // 创建测试动画
    createTestAnimations() {
        const times = [0, 1];  // 关键帧时间点
        
        // 将mesh添加为this的子节点
        this.add(this.mesh);
        
        // 待机动画
        const idleValues = [0, 0.1];  // Y轴上下浮动
        const idleTrack = new THREE.NumberKeyframeTrack(
            '.position[y]',  // 使用相对路径
            times,
            idleValues
        );
        const idleClip = new THREE.AnimationClip('idle', 1, [idleTrack]);
        this.animations.addAnimation('idle', idleClip, {
            loop: THREE.LoopRepeat
        });
        
        // 攻击动画
        const attackValues = [0, Math.PI / 2];  // 武器旋转
        const attackTrack = new THREE.NumberKeyframeTrack(
            'weapon.rotation[z]',  // 使用相对路径
            [0, 0.2],
            attackValues
        );
        const attackClip = new THREE.AnimationClip('attack', 0.2, [attackTrack]);
        this.animations.addAnimation('attack', attackClip, {
            loop: THREE.LoopOnce,
            clampWhenFinished: true,
            oneShot: true,
            returnTo: 'idle'
        });
        
        // 添加移动动画
        const walkValues = [0, 0.1, 0, -0.1];  // 上下摆动
        const walkTrack = new THREE.NumberKeyframeTrack(
            '.position[y]',
            [0, 0.25, 0.5, 0.75],
            walkValues
        );
        const walkClip = new THREE.AnimationClip('walk', 1, [walkTrack]);
        this.animations.addAnimation('walk', walkClip, {
            loop: THREE.LoopRepeat
        });
        
        // 播放默认动画
        this.animations.play('idle');
    }

    // 修改播放动画方法
    playAnimation(name, options = {}) {
        this.animations.play(name, options);
    }

    // 移动方法
    move(direction) {
        this.moveDirection.copy(direction);
        this.isMoving = direction.length() > 0;

        // 如果在移动，播放移动动画
        if (this.isMoving && !this.isAttacking) {
            this.playAnimation('walk');
        } else if (!this.isMoving && !this.isAttacking) {
            this.playAnimation('idle');
        }
    }

    // 修改tick方法
    tick(deltaTime) {
        // 更新动画系统
        this.animations.update(deltaTime);

        // 更新位置
        if (this.isMoving) {
            const movement = this.moveDirection.clone().multiplyScalar(this.moveSpeed);
            this.position.add(movement);
        }

        // 处理攻击动画结束
        if (this.isAttacking) {
            this.animationTime += deltaTime;
            if (this.animationTime >= this.attackDuration) {
                this.isAttacking = false;
                this.animationTime = 0;
                this.playAnimation(this.isMoving ? 'walk' : 'idle');
            }
        }

        // 更新物理
        if (this.body && this.mesh) {
            this.mesh.position.copy(this.position);
            this.mesh.quaternion.copy(this.quaternion);
        }
    }

    // 重写攻击方法
    attack() {
        if (this.isAttacking) return;
        
        this.isAttacking = true;
        this.animationTime = 0;
        this.playAnimation('attack');
    }
}

export { TestCharacter }; 